﻿#include "hzpch.h"
#include "OpenGLRendererAPI.h"

#include <glad/glad.h>

namespace Hazal
{
	void COpenGLRendererAPI::Init()
	{
		/// 支持混合
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

		/// 支持深度测试
		glEnable(GL_DEPTH_TEST);

	}

	void COpenGLRendererAPI::SetViewport(uint32_t unWidth, uint32_t unHeight)
	{
		glViewport(0, 0, unWidth, unHeight);
	}

	void COpenGLRendererAPI::SetClearColor(const glm::vec4& vecColor)
	{
		glClearColor(vecColor.r, vecColor.g, vecColor.b, vecColor.a);
	}

	void COpenGLRendererAPI::Clear()
	{
		/// 清除颜色缓冲和深度缓冲
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	}

	void COpenGLRendererAPI::DrawIndexed(Ref<CVertexArray>& pVertexArray, uint32_t unCount)
	{
		pVertexArray->Bind();

		uint32_t unIdxCount = unCount ? unCount : pVertexArray->GetIndexBuffer()->GetCount();
		glDrawElements(GL_TRIANGLES, unIdxCount, GL_UNSIGNED_INT, nullptr);
		glBindTexture(GL_TEXTURE_2D, 0);
	}
}